package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUnionData;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 */
public class PeakSeacraftCrossUnionDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossUnionData> {
    public static String getPeakSeacraftCrossUnionDataSql = "select * from `t_c_activity_peak_seacraft_union` where `activityId`=? and unionGroup=?";
    public Map<String, PeakSeacraftCrossUnionData> getPeakSeacraftCrossUnionDataMap(int activityId, int unionGroup) {
        Map<String, PeakSeacraftCrossUnionData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossUnionData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<String, PeakSeacraftCrossUnionData> getAllPeakSeacraftCrossUnionDataMap(int activityId) {
        String sql = "select * from `t_c_activity_peak_seacraft_union` where `activityId`=?";
        Map<String, PeakSeacraftCrossUnionData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossUnionData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePeakSeacraftCrossUnionDataSql = "update `t_c_activity_peak_seacraft_union` set round=?,groupNum=?,groupRank=?,rank=?,resultMap=?,totalDonationExp=?,memberSet=?,fixDurableMap=?,unionBaseInfo=?,totalAbility=?,abilityRank=?,score=?,area=? where activityId=? and unionGroup=? and unionId=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossUnionData peakSeacraftCrossUnionData) {
        peakSeacraftCrossUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getGroupNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getGroupRank()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getResultMap())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getTotalDonationExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getFixDurableMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionData.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getAbilityRank()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getArea()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossUnionDataSql = "insert into `t_c_activity_peak_seacraft_union`(activityId,unionGroup,unionId,round,groupNum,groupRank,rank,resultMap,totalDonationExp,memberSet,fixDurableMap,unionBaseInfo,totalAbility,abilityRank,score,area) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossUnionData peakSeacraftCrossUnionData) {
        peakSeacraftCrossUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getGroupNum()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getGroupRank()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getResultMap())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getTotalDonationExp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getFixDurableMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionData.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getAbilityRank()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionData.getArea()));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossUnionData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossUnionData peakSeacraftCrossUnionData = new PeakSeacraftCrossUnionData();
        peakSeacraftCrossUnionData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossUnionData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossUnionData.setUnionId(rs.getString("unionId"));
        peakSeacraftCrossUnionData.setRound(rs.getInt("round"));
        peakSeacraftCrossUnionData.setGroupNum(rs.getInt("groupNum"));
        peakSeacraftCrossUnionData.setGroupRank(rs.getInt("groupRank"));
        peakSeacraftCrossUnionData.setRank(rs.getInt("rank"));
        peakSeacraftCrossUnionData.setResultMap(JSON.parseObject(rs.getString("resultMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftResultInfo>>() {}));
        peakSeacraftCrossUnionData.setTotalDonationExp(rs.getInt("totalDonationExp"));
        peakSeacraftCrossUnionData.setMemberMap(JSON.parseObject(rs.getString("memberSet"), new TypeReference<ConcurrentHashMap<java.lang.Long, Integer>>() {}));
        peakSeacraftCrossUnionData.setFixDurableMap(JSON.parseObject(rs.getString("fixDurableMap"), new TypeReference<ConcurrentHashMap<java.lang.Long, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftFixDurableInfo>>() {}));
        peakSeacraftCrossUnionData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        peakSeacraftCrossUnionData.setTotalAbility(new BigInteger(rs.getString("totalAbility")));
        peakSeacraftCrossUnionData.setAbilityRank(rs.getInt("abilityRank"));
        peakSeacraftCrossUnionData.setScore(rs.getInt("score"));
        peakSeacraftCrossUnionData.setArea(rs.getInt("area"));

        return peakSeacraftCrossUnionData;
    }
}
